﻿using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dsconvert
{
   
        public class SqlHierarchicalDataSourceView : HierarchicalDataSourceView
        {
            private string viewPath;
            private IDataSource owner;
            private string _parentid;
            private string _datatid;

            public SqlHierarchicalDataSourceView(IDataSource owner, string viewPath, string parentid, string datatid)
            {
                this.viewPath = viewPath;
                this.owner = owner;
                this._parentid = parentid;
                this._datatid = datatid;
            }


            public override IHierarchicalEnumerable Select()
            {
                DataView dv = (DataView) ((this.owner) as SqlDataSource).Select(DataSourceSelectArguments.Empty);

                SqlHierarchicalEnumerable data = new SqlHierarchicalEnumerable();

                bool hasParent = false;

                foreach (DataRowView crow in dv)
                {
                    object[] array1 = crow.Row.ItemArray;
                    hasParent = false;

                    foreach (DataRowView prow in dv)
                    {
                        object[] array2 = prow.Row.ItemArray;
                        //子节点
                        string a = crow[this._parentid].ToString();
                        //根节点
                        string b = prow[this._datatid].ToString();
                        if (a == b)
                        {
                            hasParent = true;
                            break;
                        }
                    }
                    //添加根节点
                    if (!hasParent)
                        data.Add(new SqlHierarchyData(this._parentid, this._datatid, crow));
                }

                return data;
            }

        }
    }
